package com.big.common.mybatis.base.controller;

import com.big.common.core.result.PageQuery;
import com.big.common.core.result.Query;
import com.big.common.core.result.Result;
import com.big.common.log.annotation.Log;
import com.big.common.log.enums.OperationType;
import com.big.common.mybatis.base.domain.entity.BaseEntity;
import com.big.common.mybatis.base.service.BaseService;
import com.big.common.mybatis.result.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 基础控制器
 * </p>
 *
 * @author 山归岿
 * @create 2025-06-21-15:00
 */
public abstract class BaseController<DTO, T extends BaseEntity, Service extends BaseService<DTO, T>> {
    protected final Service service;

    public BaseController(Service service) {
        this.service = service;
    }

    @Operation(summary = "分页查询列表")
    @PostMapping("/page")
    @Log(module = "通用", description = "分页查询列表", type = OperationType.SELECT)
    public Result<PageResult<DTO>> page(@RequestBody PageQuery<DTO> pageQuery) {
        return Result.ok(service.page(pageQuery));
    }

    @Operation(summary = "查询列表")
    @PostMapping("/list")
    @Log(module = "通用", description = "查询列表", type = OperationType.SELECT)
    public Result<List<DTO>> listDTO(@RequestBody Query<DTO> query) {
        return Result.ok(service.listDTO(query));
    }

    @Operation(summary = "查询详情")
    @GetMapping("/{id}")
    @Log(module = "通用", description = "查询详情", type = OperationType.SELECT)
    public Result<DTO> getByIdDTO(@PathVariable("id") String id) {
        return Result.ok(service.getByIdDTO(id));
    }

    @Operation(summary = "添加")
    @PostMapping
    @Log(module = "通用", description = "添加", type = OperationType.INSERT)
    public Result<String> add(@RequestBody DTO dto) {
        return Result.ok(service.add(dto));
    }

    @Operation(summary = "批量添加")
    @PostMapping("/batch")
    @Log(module = "通用", description = "批量添加", type = OperationType.INSERT)
    public Result<Boolean> addBatch(@RequestBody List<DTO> dtoList) {
        return Result.ok(service.addBatch(dtoList, dtoList.size()));
    }

    @Operation(summary = "更新")
    @PutMapping
    @Log(module = "通用", description = "更新", type = OperationType.UPDATE)
    public Result<String> update(@RequestBody DTO dto) {
        return Result.ok(service.update(dto));
    }

    @Operation(summary = "批量更新")
    @PutMapping("/batch")
    @Log(module = "通用", description = "批量更新", type = OperationType.UPDATE)
    public Result<Boolean> updateBatch(@RequestBody List<DTO> dtoList) {
        return Result.ok(service.updateBatch(dtoList, dtoList.size()));
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Log(module = "通用", description = "删除", type = OperationType.DELETE)
    public Result<Boolean> delete(@PathVariable("id") String id) {
        return Result.ok(service.delete(id));
    }

    @Operation(summary = "批量删除")
    @DeleteMapping
    @Log(module = "通用", description = "批量删除", type = OperationType.DELETE)
    public Result<Boolean> deleteBatch(@RequestBody List<String> ids) {
        return Result.ok(service.deleteBatch(ids));
    }
}
